var init = 0; var init_bgm = 0;
var init2 = 0;
var innerAudioContext;//背景音乐对象
var button;//按钮音乐对象
var timer = 0;//设置定时器
var time;//定时器赋值
// 进入游戏界面
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowWidth: '',
    windowHeight: '',
    animation: '',
    guideAnimation:{},
    code:0,//用户是否第一次进入
    aniDisplay:"none"//指导动画显示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取设备信息。
    let vm = this
    //获取code值
    var code = wx.getStorageSync('code')
    vm.setData({
      code: code
    })
    wx.getSystemInfo({
      success: function (res) {
        vm.setData({          
          windowWidth: res.windowWidth//可使用宽度
        })
        vm.setData({
          windowHeight: res.screenHeight//可使用高度
        })
      }
    })
  },
  startbgAudio: function (u, a, l) {
    if (init == 0) {
      init++;
      innerAudioContext = wx.createInnerAudioContext()
      innerAudioContext.autoplay = a;
      var urlKey = u;
      var savedFilePath = wx.getStorageSync(urlKey);
      innerAudioContext.src = savedFilePath;
      innerAudioContext.loop = l;
    } else {
      innerAudioContext.autoplay = a;
      innerAudioContext.src = wx.getStorageSync(u);
      innerAudioContext.loop = l;
    }
  },
  stopbgAudio:function(){
    innerAudioContext.stop();
  },
  button: function (u, a, l) {
    if (init2 == 0) {
      init2++;
      button = wx.createInnerAudioContext()
      button.autoplay = a;
      var urlKey = u;
      var savedFilePath = wx.getStorageSync(urlKey);
      button.src = savedFilePath;
      button.loop = l;
    } else {
      button.autoplay = a;
      button.src = wx.getStorageSync(u);
      button.loop = l;
    }
  },
  //开始游戏
  startGame() {
    this.button("button1", true, false);
    wx.redirectTo({
      //跳转
      url: '../level/level?code=' + this.data.code,
    })
  },
  onReady: function () {
    // 页面渲染完成
    //实例化一个动画
    this.animation = wx.createAnimation({
      // 动画持续时间，单位ms，默认值 400
      duration: 500,
      timingFunction: 'linear',
      // 延迟多长时间开始
      delay: 100,
    })
    this.trans()
  },
  onShow:function(){
    let vm=this    
    if(vm.data.code==0){
      guide(vm)
  
    }
  },
  //标志掉落动画
  trans: function () {
    //顺时针旋转10度
    this.animation.translateY(220).step()
    this.setData({
      //输出动画
      animation: this.animation.export()
    })
  },
  guide:function(){
    //指导动画
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: "ease",
    })
    animation.scale(0.8, 0.8).rotate(10).opacity(0.5).step();
    this.setData({
    guideAnimation: animation.export()
    })
    console.log("*******************")
    setTimeout(function () {
      animation.translate(30, 30).step();
      this.setData({
        guideAnimation: animation.export()
      })
    }.bind(this), 1000)
  },
  //关闭界面,清除定时器
  onUnload: function (){
    clearInterval(time)
  },
})

function guide(that){
    let vm=that
    vm.setData({
      aniDisplay: "block"//显示指导教程
    })
    var guideAnimation = wx.createAnimation({
      duration: 100, // 默认为400     动画持续时间，单位ms
      timingFunction: 'ease-in-out',
      delay: 100,
    });
    time=setInterval(function () {
      //动画的脚本定义必须每次都重新生成，不能放在循环外
      guideAnimation.translate(40, 40).step({ duration: 1000 }).translate().step({ duration: 1000 });
      // 更新数据
      vm.setData({
        // 导出动画示例
        //animationData: animationData.export(),
        guideAnimation: guideAnimation.export(),
      })
      ++timer;
      console.log(timer);
    }.bind(vm), 2000);
}